package com.hhxy.web;

import com.hhxy.utils.CheckCodeUtil;

import javax.servlet.ServletException;
import javax.servlet.ServletOutputStream;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;

@WebServlet("/CheckCodeServlet")
//响应验证码
public class CheckCodeServlet extends HttpServlet {
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        this.doGet(request, response);
    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        //1、获取Response字节输出流
        ServletOutputStream sos = response.getOutputStream();
        //2、调用工具类的方法生成验证码，并将验证码响应到网页上
        String authCode = CheckCodeUtil.outputVerifyImage(100, 50, sos, 4);
        //3、将验证码存入Session中，用于提交后的比对
        HttpSession session = request.getSession();
        session.setAttribute("authCode",authCode);
        System.out.println(authCode);
    }
}
